package org.ala.shopping_test.shopping_center.repository.order_buyer.router;

import java.util.List;
import java.util.Map;

import org.ala.shopping_test.shopping_center.repository.order_buyer.dao.ShoppingOrderBuyerMapper.MultipleUpdate;
import org.ala.shopping_test.shopping_center.repository.order_buyer.query.ShoppingOrderBuyerQuery;
import org.ala.shopping_test.shopping_center.repository.shard_key.ShoppingOrderBuyerSellerShardKey;
import org.ala.shopping_test.shopping_center.repository.shard_key.ShoppingOrderBuyerSellerShardKeyRouter;
import org.ala.tiktools.sharddb.mybatis.router.IShardRouter;
import org.ala.tiktools.sharddb.mybatis.router.ShardInfo;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKeyConverter;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKeyRouter;
import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.ShardKeyRouteWorker;

/**
 *	分库分表路由器 (shopping_order_buyer)
 *  订单总表
 *
 *	@author 骆毅 (ala)
 *	@date 2025-03-21 12:09:46
 */
public class ShoppingOrderBuyerMultipleUpdateListRouter implements IShardRouter<List<MultipleUpdate>> {
    /**
     *  分片建路由器
     */
    protected IShardKeyRouter<ShoppingOrderBuyerSellerShardKey> router = new ShoppingOrderBuyerSellerShardKeyRouter();
    /**
     *  分片建转换器
     */
    protected IShardKeyConverter<MultipleUpdate, ShoppingOrderBuyerSellerShardKey> converter = new IShardKeyConverter<MultipleUpdate, ShoppingOrderBuyerSellerShardKey>() {
        @Override
        public ShoppingOrderBuyerSellerShardKey convert(MultipleUpdate d) {
            return ShoppingOrderBuyerSellerShardKey.build()
                    .id(d.getQuery().getBuyerId())
                    .timestamp(d.getQuery().getCreateTime())
                    .timestampGte(d.getQuery().getCreateTimeGte() != null ? d.getQuery().getCreateTimeGte() : (d.getQuery().getCreateTimeGt() != null ? d.getQuery().getCreateTimeGt() + 1 : -1))
                    .timestampLte(d.getQuery().getCreateTimeLte() != null ? d.getQuery().getCreateTimeLte() : (d.getQuery().getCreateTimeLt() != null ? d.getQuery().getCreateTimeLt() -1 : -1));
        }

        @Override
        public MultipleUpdate deConvert(MultipleUpdate d, ShoppingOrderBuyerSellerShardKey k) {
            MultipleUpdate _d = d.copy();
            ShoppingOrderBuyerQuery q = d.getQuery();
            q.buyerId(k.getId())
                    .createTime(k.getTimestamp() > 0 ? k.getTimestamp() : null)
                    .createTimeGte(k.getTimestampGte() > 0 ? k.getTimestampGte() : null)
                    .createTimeLte(k.getTimestampLte() > 0 ? k.getTimestampLte() : null);
            return _d;
        }
    };


    /**
     * 分库分表
     */
    @Override
    public Map<ShardInfo, List<MultipleUpdate>> route(List<MultipleUpdate> d) {
        return ShardKeyRouteWorker.instance().route(d, router, converter);
    }
}